package adapter

import (
	"fmt"
	"github.com/stretchr/testify/assert"
	"migration-tool/common"
	"migration-tool/target"
	"sync"
	"testing"
	"time"
)

// 不分库测试函数
func getUseProgressConfig(t *testing.T) *common.Config {
	cfg, err := common.ParseConfig(TestDefaultConfigPath)
	assert.Equal(t, err, nil)
	cfg.BaseConfig.UseProgress = true
	cfg.BaseConfig.Export = true
	log, err := common.NewGlobalLog(cfg.BaseConfig.ExportDataDir + "/test-global.log")
	assert.Equal(t, err, nil)
	cfg.GlobalLog = log
	return &cfg
}

// 不分库测试函数
func getNotUseProgressConfig(t *testing.T) *common.Config {
	cfg, err := common.ParseConfig(TestDefaultConfigPath)
	assert.Equal(t, err, nil)
	cfg.BaseConfig.UseProgress = false
	cfg.BaseConfig.Export = true
	log, err := common.NewGlobalLog(cfg.BaseConfig.ExportDataDir + "/test-global.log")
	assert.Equal(t, err, nil)
	cfg.GlobalLog = log
	return &cfg
}

// 不分库测试函数
func TestGiteeGetAllExportRepoInfoNoEnterpriseToken(t *testing.T) {
	cfg := getUseProgressConfig(t)
	cfg.BaseConfig.EnterpriseToken = ""
	cfg.BaseConfig.EnterpriseName = "anolis"
	repos, err := giteeGetAllRepoInfoForExportNoEnterpriseToken(cfg)
	assert.Equal(t, err, nil)
	fmt.Println(len(repos))
	//for _, val := range repos {
	//	fmt.Println(val)
	//}
}

// 不分库测试函数
func TestGiteeGetAllExportRepoInfo(t *testing.T) {
	cfg := getUseProgressConfig(t)
	repos, err := giteeGetAllRepoInfoForExport(cfg)
	assert.Equal(t, err, nil)
	fmt.Println(len(repos))
	//for _, val := range repos {
	//	fmt.Println(val)
	//}
}

// 不分库测试函数
func TestGiteeGetAllImportRepoInfo(t *testing.T) {
	cfg := getUseProgressConfig(t)
	repos, err := giteeGetAllRepoInfoForImport(cfg)
	assert.Equal(t, err, nil)
	fmt.Println(len(repos))
	//for _, val := range repos {
	//	fmt.Println(val)
	//}
}

// Gitee不分库全量导出单元测试——Enterprise
func TestGiteeDealEnterprise(t *testing.T) {
	cfg := getUseProgressConfig(t)
	repoInfo := RepoInfo{
		RepoId:        7696525,
		RepoName:      "kernel",
		RepoGroupId:   5161228,
		RepoGroupName: "openEuler",
		//RepoName:         "aops-apollo",
		EnterpriseId:     5292411,
		CodeCloneHttpUrl: "https://gitee.com/openeuler/kernel.git",
		WikiEnable:       true,
	}

	args := TaskArgs{
		TaskName:        GlobalTaskNameEnterprise,
		RepoInfo:        &repoInfo,
		ExportDataDir:   cfg.BaseConfig.ExportDataDir,
		Log:             cfg.GlobalLog,
		Token:           cfg.BaseConfig.Token,
		EnterpriseToken: cfg.BaseConfig.EnterpriseToken,
		EnterpriseName:  cfg.BaseConfig.EnterpriseName,
		PageSize:        cfg.BaseConfig.PageSize,
	}

	err := globalGiteeDealEnterprise(args)
	assert.Equal(t, err, nil)

	var enterprise target.AtomGitEnterprise
	dataArgs := DataArgs{
		ExportDataDir:      args.ExportDataDir,
		FileName:           BinFileNameEnterprise,
		ExportPlatformName: GiteePlatformName,
		EnterpriseName:     args.EnterpriseName,
		FilePathTmpl:       ExportEnterpriseFileTmpl,
		Data:               &enterprise,
	}

	_, err = ReadProtoData(dataArgs)
	assert.Equal(t, err, nil)
	fmt.Println(enterprise.String())

	var memberList target.AtomGitEnterpriseMemberList
	dataArgs = DataArgs{
		ExportDataDir:      args.ExportDataDir,
		FileName:           BinFileNameEnterpriseMembersProto,
		ExportPlatformName: GiteePlatformName,
		EnterpriseName:     args.EnterpriseName,
		FilePathTmpl:       ExportEnterpriseFileTmpl,
		Data:               &memberList,
	}

	_, err = ReadProtoData(dataArgs)
	assert.Equal(t, err, nil)

	fmt.Println(len(memberList.MemberList))
	for _, val := range memberList.MemberList {
		fmt.Println(val.User.Id, val.User.Name, val.User.Phone, val.User.Email, val.AccessLevel)
	}

}

// Gitee不分库全量导出单元测试——Group
func TestGiteeDealGroup(t *testing.T) {
	cfg := getNotUseProgressConfig(t)
	cfg.BaseConfig.UseProgress = true
	_, err := giteeGetAllRepoInfoForExport(cfg)
	cfg.BaseConfig.UseProgress = false
	repoInfo := RepoInfo{
		RepoId:        7696525,
		RepoName:      "kernel",
		RepoGroupId:   5161228,
		RepoGroupName: "openEuler",
		//RepoName:         "aops-apollo",
		EnterpriseId:     5292411,
		CodeCloneHttpUrl: "https://gitee.com/openeuler/kernel.git",
		WikiEnable:       true,
	}

	args := TaskArgs{
		TaskName:        GlobalTaskNameGroup,
		RepoInfo:        &repoInfo,
		ExportDataDir:   cfg.BaseConfig.ExportDataDir,
		Log:             cfg.GlobalLog,
		Token:           cfg.BaseConfig.Token,
		EnterpriseToken: cfg.BaseConfig.EnterpriseToken,
		EnterpriseName:  cfg.BaseConfig.EnterpriseName,
		PageSize:        cfg.BaseConfig.PageSize,
	}

	err = globalGiteeDealGroup(args)
	assert.Equal(t, err, nil)

	var groupList target.AtomGitGroupList
	dataArgs := DataArgs{
		ExportDataDir:      args.ExportDataDir,
		FileName:           BinFileNameGroups,
		ExportPlatformName: GiteePlatformName,
		EnterpriseName:     args.EnterpriseName,
		FilePathTmpl:       ExportEnterpriseFileTmpl,
		Data:               &groupList,
		WriteJson:          true,
	}

	_, err = ReadProtoData(dataArgs)
	assert.Equal(t, err, nil)
	fmt.Println(len(groupList.GroupList))
	for _, val := range groupList.GroupList {
		fmt.Println(val.Id, val.Name, val.Description, val.Creator)
	}

	var memberList target.AtomGitGroupMemberList
	dataArgs = DataArgs{
		ExportDataDir:      args.ExportDataDir,
		FileName:           BinFileNameGroupMembers,
		ExportPlatformName: GiteePlatformName,
		EnterpriseName:     args.EnterpriseName,
		FilePathTmpl:       ExportEnterpriseFileTmpl,
		Data:               &memberList,
	}

	_, err = ReadProtoData(dataArgs)
	assert.Equal(t, err, nil)

	fmt.Println(len(memberList.MemberList))
	for _, val := range memberList.MemberList {
		fmt.Println(val.User.Id, val.User.Phone, val.GroupName, val.AccessLevel)
	}

}

// Gitee不分库全量导出单元测试——Label
func TestGiteeDealLabel(t *testing.T) {
	cfg := getUseProgressConfig(t)
	repoInfo := RepoInfo{
		RepoId:        7696525,
		RepoName:      "kernel",
		RepoGroupId:   5161228,
		RepoGroupName: "openEuler",
		//RepoName:         "aops-apollo",
		EnterpriseId:     5292411,
		CodeCloneHttpUrl: "https://gitee.com/openeuler/kernel.git",
		WikiEnable:       true,
	}

	args := TaskArgs{
		TaskName:        GlobalTaskNameLabel,
		RepoInfo:        &repoInfo,
		ExportDataDir:   cfg.BaseConfig.ExportDataDir,
		Log:             cfg.GlobalLog,
		Token:           cfg.BaseConfig.Token,
		EnterpriseToken: cfg.BaseConfig.EnterpriseToken,
		EnterpriseName:  cfg.BaseConfig.EnterpriseName,
		PageSize:        cfg.BaseConfig.PageSize,
	}

	err := globalGiteeDealLabel(args)
	assert.Equal(t, err, nil)

	var labelList target.AtomGitLabelList
	dataArgs := DataArgs{
		ExportDataDir:      args.ExportDataDir,
		FileName:           BinFileNameLabels,
		ExportPlatformName: GiteePlatformName,
		EnterpriseName:     args.EnterpriseName,
		FilePathTmpl:       ExportEnterpriseFileTmpl,
		Data:               &labelList,
	}

	_, err = ReadProtoData(dataArgs)
	assert.Equal(t, err, nil)

	fmt.Println(len(labelList.LabelList))
	for _, val := range labelList.LabelList {
		fmt.Println(val.Id, val.Name, val.Color)
	}
}

// Gitee分库全量导出单元测试——MileStone
func TestGiteeDealMileStone(t *testing.T) {
	cfg := getUseProgressConfig(t)
	repoInfo := RepoInfo{
		RepoId:        7696525,
		RepoName:      "kernel",
		RepoGroupId:   5161228,
		RepoGroupName: "openEuler",
		//RepoName:         "aops-apollo",
		EnterpriseId:     5292411,
		CodeCloneHttpUrl: "https://gitee.com/openeuler/kernel.git",
		WikiEnable:       true,
	}

	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, GiteePlatformName)
	assert.Equal(t, err, nil)

	args := TaskArgs{
		TaskName:        TaskNameMileStone,
		RepoInfo:        &repoInfo,
		ExportDataDir:   cfg.BaseConfig.ExportDataDir,
		Log:             log,
		Token:           cfg.BaseConfig.Token,
		EnterpriseToken: cfg.BaseConfig.EnterpriseToken,
		PageSize:        cfg.BaseConfig.PageSize,
	}
	err = giteeDealMileStone(args)
	assert.Equal(t, err, nil)

	var mileStoneList target.AtomGitMileStoneList
	dataArgs := DataArgs{
		RepoInfo:           &repoInfo,
		ExportDataDir:      args.ExportDataDir,
		FileName:           BinFileNameMileStones,
		ExportPlatformName: GiteePlatformName,
		FilePathTmpl:       ExportDataFileTmpl,
		Data:               &mileStoneList,
	}

	_, err = ReadProtoData(dataArgs)
	assert.Equal(t, err, nil)

	fmt.Println(len(mileStoneList.MileStoneList))
	for _, val := range mileStoneList.MileStoneList {
		fmt.Println(val.Id, val.Description, val.Title)
	}
}

// Gitee分库全量导出单元测试——RepoMember
func TestGiteeDealRepoMember(t *testing.T) {
	cfg := getUseProgressConfig(t)
	repoInfo := RepoInfo{
		RepoId:        7696525,
		RepoName:      "kernel",
		RepoGroupId:   5161228,
		RepoGroupName: "openEuler",
		//RepoName:         "aops-apollo",
		EnterpriseId:     5292411,
		CodeCloneHttpUrl: "https://gitee.com/openeuler/kernel.git",
		WikiEnable:       true,
	}

	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, GiteePlatformName)
	assert.Equal(t, err, nil)

	args := TaskArgs{
		TaskName:        TaskNameRepoMember,
		RepoInfo:        &repoInfo,
		ExportDataDir:   cfg.BaseConfig.ExportDataDir,
		Log:             log,
		Token:           cfg.BaseConfig.Token,
		EnterpriseToken: cfg.BaseConfig.EnterpriseToken,
		PageSize:        cfg.BaseConfig.PageSize,
	}
	err = giteeDealRepoMember(args)
	assert.Equal(t, err, nil)

	var memberList target.AtomGitRepoMemberList
	dataArgs := DataArgs{
		RepoInfo:           &repoInfo,
		ExportDataDir:      args.ExportDataDir,
		FileName:           BinFileNameRepoMembers,
		ExportPlatformName: GiteePlatformName,
		FilePathTmpl:       ExportDataFileTmpl,
		Data:               &memberList,
	}

	_, err = ReadProtoData(dataArgs)
	assert.Equal(t, err, nil)

	fmt.Println(len(memberList.MemberList))
	for _, val := range memberList.MemberList {
		fmt.Println(val.AccessLevel, val.User)
	}
}

// Gitee分库全量导出单元测试——ProtectBranchSetting
func TestGiteeDealProtectBranchSetting(t *testing.T) {
	cfg := getUseProgressConfig(t)
	repoInfo := RepoInfo{
		RepoId:        7696525,
		RepoName:      "kernel",
		RepoGroupId:   5161228,
		RepoGroupName: "openEuler",
		//RepoName:         "aops-apollo",
		EnterpriseId:     5292411,
		CodeCloneHttpUrl: "https://gitee.com/openeuler/kernel.git",
		WikiEnable:       true,
	}

	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, GiteePlatformName)
	assert.Equal(t, err, nil)

	args := TaskArgs{
		TaskName:        TaskNameProtectBranchSetting,
		RepoInfo:        &repoInfo,
		ExportDataDir:   cfg.BaseConfig.ExportDataDir,
		Log:             log,
		Token:           cfg.BaseConfig.Token,
		EnterpriseToken: cfg.BaseConfig.EnterpriseToken,
		PageSize:        cfg.BaseConfig.PageSize,
	}
	err = giteeDealProtectBranchSetting(args)
	assert.Equal(t, err, nil)

	var settingList target.AtomGitProtectBranchSettingList
	dataArgs := DataArgs{
		RepoInfo:           &repoInfo,
		ExportDataDir:      args.ExportDataDir,
		FileName:           BinFileNameProtectBranchSetting,
		ExportPlatformName: GiteePlatformName,
		FilePathTmpl:       ExportDataFileTmpl,
		Data:               &settingList,
	}

	_, err = ReadProtoData(dataArgs)
	assert.Equal(t, err, nil)

	fmt.Println(len(settingList.ProtectBranchSettingList))
	for _, val := range settingList.ProtectBranchSettingList {
		fmt.Println(val)
	}
}

// Gitee分库全量导出单元测试——RepoMetaInfo
func TestGiteeDealRepoMetaInfo(t *testing.T) {
	cfg := getUseProgressConfig(t)
	//cfg.BaseConfig.EnterpriseToken = ""
	//cfg.BaseConfig.EnterpriseName = "anolis"
	//repoInfo := RepoInfo{
	//	RepoId:        7696525,
	//	RepoName:      "kernel",
	//	RepoGroupId:   5161228,
	//	RepoGroupName: "openEuler",
	//	//RepoName:         "aops-apollo",
	//	EnterpriseId:     5292411,
	//	CodeCloneHttpUrl: "https://gitee.com/openeuler/kernel.git",
	//	WikiEnable:       true,
	//}

	repoInfo := RepoInfo{
		RepoId: 25286931,
		//RepoName:      "kernel",
		RepoGroupId:      5161228,
		RepoGroupName:    "openEuler",
		RepoName:         "aops-apollo",
		EnterpriseId:     5292411,
		CodeCloneHttpUrl: "https://gitee.com/openeuler/aops-apollo.git",
		WikiEnable:       true,
	}

	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, GiteePlatformName)
	assert.Equal(t, err, nil)

	args := TaskArgs{
		TaskName:        TaskNameRepoMetaInfo,
		RepoInfo:        &repoInfo,
		ExportDataDir:   cfg.BaseConfig.ExportDataDir,
		Log:             log,
		Token:           cfg.BaseConfig.Token,
		EnterpriseToken: cfg.BaseConfig.EnterpriseToken,
		PageSize:        cfg.BaseConfig.PageSize,
	}

	err = giteeDealRepoMetaInfo(args)
	assert.Equal(t, err, nil)

	var data target.AtomGitRepoMetaInfo
	dataArgs := DataArgs{
		RepoInfo:           &repoInfo,
		ExportDataDir:      args.ExportDataDir,
		FileName:           BinFileNameRepoMetaInfo,
		ExportPlatformName: GiteePlatformName,
		FilePathTmpl:       ExportDataFileTmpl,
		Data:               &data,
	}

	_, err = ReadProtoData(dataArgs)
	assert.Equal(t, err, nil)
	fmt.Println(data.String())
	fmt.Println(data.PathWithNamespace)
	fmt.Println(data.NameWithNamespace)
}

// Gitee分库全量导出单元测试——Release
func TestGiteeDealRelease(t *testing.T) {
	cfg := getUseProgressConfig(t)
	//repoInfo := RepoInfo{
	//	RepoId:        7696525,
	//	RepoName:      "kernel",
	//	RepoGroupId:   5161228,
	//	RepoGroupName: "openEuler",
	//	//RepoName:         "aops-apollo",
	//	EnterpriseId:     5292411,
	//	CodeCloneHttpUrl: "https://gitee.com/openeuler/kernel.git",
	//	WikiEnable:       true,
	//}

	repoInfo := RepoInfo{
		RepoId:           31552509,
		RepoGroupName:    "bu_yu_0721",
		RepoName:         "test-migration",
		CodeCloneHttpUrl: "https://gitee.com/bu_yu_0721/test-migration.git",
		EnterpriseId:     5292411,
		WikiEnable:       true,
	}

	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, GiteePlatformName)
	assert.Equal(t, err, nil)

	args := TaskArgs{
		TaskName:        TaskNameRelease,
		RepoInfo:        &repoInfo,
		ExportDataDir:   cfg.BaseConfig.ExportDataDir,
		Log:             log,
		Token:           cfg.BaseConfig.Token,
		EnterpriseToken: cfg.BaseConfig.EnterpriseToken,
		PageSize:        cfg.BaseConfig.PageSize,
	}

	err = giteeDealRelease(args)
	assert.Equal(t, err, nil)

	var data target.AtomGitReleaseList
	dataArgs := DataArgs{
		RepoInfo:           &repoInfo,
		ExportDataDir:      args.ExportDataDir,
		FileName:           BinFileNameReleases,
		ExportPlatformName: GiteePlatformName,
		FilePathTmpl:       ExportDataFileTmpl,
		Data:               &data,
	}

	_, err = ReadProtoData(dataArgs)
	assert.Equal(t, err, nil)

	for _, val := range data.ReleaseList {
		fmt.Println(val.String())
	}

}

// Gitee分库全量导出单元测试——PushRuleSetting
func TestGiteeDealPushRuleSetting(t *testing.T) {
	cfg := getUseProgressConfig(t)
	repoInfo := RepoInfo{
		RepoId:           31552509,
		RepoGroupName:    "bu_yu_0721",
		RepoName:         "test-migration",
		CodeCloneHttpUrl: "https://gitee.com/bu_yu_0721/test-migration.git",
		EnterpriseId:     5292411,
		WikiEnable:       true,
	}

	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, GiteePlatformName)
	assert.Equal(t, err, nil)

	args := TaskArgs{
		TaskName:        TaskNamePushRuleSetting,
		RepoInfo:        &repoInfo,
		ExportDataDir:   cfg.BaseConfig.ExportDataDir,
		Log:             log,
		Token:           cfg.BaseConfig.Token,
		EnterpriseToken: cfg.BaseConfig.EnterpriseToken,
		PageSize:        cfg.BaseConfig.PageSize,
	}

	err = giteeDealPushRuleSetting(args)
	assert.Equal(t, err, nil)

	var data target.AtomGitPushRuleSettingList
	dataArgs := DataArgs{
		RepoInfo:           &repoInfo,
		ExportDataDir:      args.ExportDataDir,
		FileName:           BinFileNamePushRuleSetting,
		ExportPlatformName: GiteePlatformName,
		FilePathTmpl:       ExportDataFileTmpl,
		Data:               &data,
	}

	_, err = ReadProtoData(dataArgs)
	assert.Equal(t, err, nil)

	for _, val := range data.PushRuleSettingList {
		fmt.Println(val.String())
	}
}

// Gitee分库全量导出单元测试——WebHookSetting
func TestGiteeDealWebHookSetting(t *testing.T) {
	cfg := getUseProgressConfig(t)
	repoInfo := RepoInfo{
		RepoId:           31552509,
		RepoGroupName:    "bu_yu_0721",
		RepoName:         "test-migration",
		CodeCloneHttpUrl: "https://gitee.com/bu_yu_0721/test-migration.git",
		EnterpriseId:     5292411,
		WikiEnable:       true,
	}

	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, GiteePlatformName)
	assert.Equal(t, err, nil)

	args := TaskArgs{
		TaskName:        TaskNameWebHookSetting,
		RepoInfo:        &repoInfo,
		ExportDataDir:   cfg.BaseConfig.ExportDataDir,
		Log:             log,
		Token:           cfg.BaseConfig.Token,
		EnterpriseToken: cfg.BaseConfig.EnterpriseToken,
		PageSize:        cfg.BaseConfig.PageSize,
	}

	err = giteeDealWebHookSetting(args)
	assert.Equal(t, err, nil)

	var data target.AtomGitWebHookSettingList
	dataArgs := DataArgs{
		RepoInfo:           &repoInfo,
		ExportDataDir:      args.ExportDataDir,
		FileName:           BinFileNameWebHookSetting,
		ExportPlatformName: GiteePlatformName,
		FilePathTmpl:       ExportDataFileTmpl,
		Data:               &data,
	}

	_, err = ReadProtoData(dataArgs)
	assert.Equal(t, err, nil)

	for _, val := range data.WebHookList {
		fmt.Println(val.String())
	}
}

// Gitee分库全量导出单元测试——DeployKeySetting
func TestGiteeDealDeployKeySetting(t *testing.T) {
	cfg := getUseProgressConfig(t)
	repoInfo := RepoInfo{
		RepoId:           31552509,
		RepoGroupName:    "bu_yu_0721",
		RepoName:         "test-migration",
		CodeCloneHttpUrl: "https://gitee.com/bu_yu_0721/test-migration.git",
		EnterpriseId:     5292411,
		WikiEnable:       true,
	}

	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, GiteePlatformName)
	assert.Equal(t, err, nil)

	args := TaskArgs{
		TaskName:        TaskNameDeployKeysSetting,
		RepoInfo:        &repoInfo,
		ExportDataDir:   cfg.BaseConfig.ExportDataDir,
		Log:             log,
		Token:           cfg.BaseConfig.Token,
		EnterpriseToken: cfg.BaseConfig.EnterpriseToken,
		PageSize:        cfg.BaseConfig.PageSize,
	}

	err = giteeDealDeployKeySetting(args)
	assert.Equal(t, err, nil)

	var data target.AtomGitDeployKeysSettingList
	dataArgs := DataArgs{
		RepoInfo:           &repoInfo,
		ExportDataDir:      args.ExportDataDir,
		FileName:           BinFileNameDeployKeysSetting,
		ExportPlatformName: GiteePlatformName,
		FilePathTmpl:       ExportDataFileTmpl,
		Data:               &data,
	}

	_, err = ReadProtoData(dataArgs)
	assert.Equal(t, err, nil)

	for _, val := range data.DeployKeysList {
		fmt.Println(val.String())
	}
}

// Gitee分库增量导出单元测试——MergeRequest
func TestGiteeDealIncrementMergeRequset(t *testing.T) {
	cfg := getNotUseProgressConfig(t)
	repoInfo := RepoInfo{
		RepoId:        7696525,
		RepoName:      "kernel",
		RepoGroupId:   5161228,
		RepoGroupName: "openEuler",
		//RepoName:         "aops-apollo",
		EnterpriseId:     5292411,
		CodeCloneHttpUrl: "https://gitee.com/openeuler/kernel.git",
		WikiEnable:       true,
	}

	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, GiteePlatformName)
	assert.Equal(t, err, nil)

	args := TaskArgs{
		TaskName:        TaskNameMergeRequest,
		RepoInfo:        &repoInfo,
		ExportDataDir:   cfg.BaseConfig.ExportDataDir,
		Log:             log,
		Token:           cfg.BaseConfig.Token,
		PageSize:        cfg.BaseConfig.PageSize,
		IncrementFlag:   true,
		EnterpriseToken: cfg.BaseConfig.EnterpriseToken,
		TimeStamp:       cfg.BaseConfig.TimeStamp,
	}

	err = giteeDealMergeRequest(args)
	fmt.Println(err)
	assert.Equal(t, err, nil)

	var mrList target.AtomGitMergeRequestList
	dataArgs := DataArgs{
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		ExportPlatformName: GiteePlatformName,
		FileName:           BinFileNameMergeRequestsIncrement,
		FilePathTmpl:       ExportDataFileTmpl,
		RepoInfo:           &repoInfo,
		Data:               &mrList,
	}

	_, err = ReadProtoData(dataArgs)
	assert.Equal(t, err, nil)

	fmt.Println(len(mrList.MergeRequestList))
	for _, val := range mrList.MergeRequestList {
		fmt.Println(val.Id, val.Title, time.Unix(val.UpdatedAt, 0), len(val.CommentList))
		//for _, cmt := range val.CommentList {
		//	fmt.Println(cmt.Id, cmt.Note, cmt.Author)
		//}
		//fmt.Println("=================")
	}
}

// Gitee分库全量导出单元测试——MergeRequest
func TestGiteeDealMergeRequset(t *testing.T) {
	cfg := getNotUseProgressConfig(t)
	repoInfo := RepoInfo{
		RepoId:        7696525,
		RepoName:      "stratovirt",
		RepoGroupId:   5161228,
		RepoGroupName: "openeuler",
		//RepoName:         "aops-apollo",
		EnterpriseId:     5292411,
		CodeCloneHttpUrl: "https://gitee.com/openeuler/kernel.git",
		WikiEnable:       true,
	}

	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, GiteePlatformName)
	assert.Equal(t, err, nil)

	args := TaskArgs{
		TaskName:        TaskNameMergeRequest,
		RepoInfo:        &repoInfo,
		ExportDataDir:   cfg.BaseConfig.ExportDataDir,
		Log:             log,
		Token:           cfg.BaseConfig.Token,
		PageSize:        cfg.BaseConfig.PageSize,
		IncrementFlag:   false,
		EnterpriseToken: cfg.BaseConfig.EnterpriseToken,
		TimeStamp:       cfg.BaseConfig.TimeStamp,
	}

	_ = args
	//err = giteeDealMergeRequest(args)
	assert.Equal(t, err, nil)

	var mrList target.AtomGitMergeRequestList
	dataArgs := DataArgs{
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		ExportPlatformName: GiteePlatformName,
		FileName:           BinFileNameMergeRequests,
		FilePathTmpl:       ExportDataFileTmpl,
		//FilePathTmpl: "/Users/buyu/test/my_repo_data/mr-cross.data",
		//FilePathTmpl: "/Users/buyu/test/my_repo_data/mr-same.data",
		//FilePathTmpl: "/Users/buyu/test/my_repo_data/mr-filepath.data",

		RepoInfo: &repoInfo,
		Data:     &mrList,
	}

	_, err = ReadProtoData(dataArgs)
	assert.Equal(t, err, nil)

	fmt.Println(len(mrList.MergeRequestList))
	fmt.Println(mrList.RepoId)
	fmt.Println(mrList.RepoName)
	fmt.Println(mrList.RepoGroupName)
	for _, val := range mrList.MergeRequestList {
		//fmt.Println(val.Id, val.Title, time.Unix(val.UpdatedAt, 0), len(val.CommentList))
		//for _, cmt := range val.CommentList {
		//	fmt.Println(cmt.Id, cmt.Note, cmt.Author)
		//}
		//fmt.Println(val)
		_ = val
		//fmt.Println("=================")
	}
}

// Gitee分库增量导出单元测试——Issue
func TestGiteeDealIncrementIssue(t *testing.T) {
	cfg := getNotUseProgressConfig(t)
	repoInfo := RepoInfo{
		RepoId:        7696525,
		RepoName:      "kernel",
		RepoGroupId:   5161228,
		RepoGroupName: "openEuler",
		//RepoName:         "aops-apollo",
		EnterpriseId:     5292411,
		CodeCloneHttpUrl: "https://gitee.com/openeuler/kernel.git",
		WikiEnable:       true,
	}

	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, GiteePlatformName)
	assert.Equal(t, err, nil)

	args := TaskArgs{
		TaskName:        TaskNameIssue,
		RepoInfo:        &repoInfo,
		ExportDataDir:   cfg.BaseConfig.ExportDataDir,
		Log:             log,
		Token:           cfg.BaseConfig.Token,
		EnterpriseToken: cfg.BaseConfig.EnterpriseToken,
		PageSize:        cfg.BaseConfig.PageSize,
		IncrementFlag:   true,
		TimeStamp:       cfg.BaseConfig.TimeStamp,
	}

	err = giteeDealIssue(args)
	assert.Equal(t, err, nil)

	var issueList target.AtomGitIssueList
	dataArgs := DataArgs{
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		ExportPlatformName: GiteePlatformName,
		FileName:           BinFileNameIssuesIncrement,
		FilePathTmpl:       ExportDataFileTmpl,
		RepoInfo:           &repoInfo,
		Data:               &issueList,
	}

	_, err = ReadProtoData(dataArgs)
	assert.Equal(t, err, nil)

	fmt.Println(len(issueList.IssueList))
	for _, val := range issueList.IssueList {
		fmt.Println(val.Id, val.Title, time.Unix(val.UpdatedAt, 0), len(val.CommentList))
		//for _, cmt := range val.CommentList {
		//	fmt.Println(cmt.Id, cmt.Note, cmt.Author)
		//}
		//fmt.Println("=================")
	}
}

// Gitee分库全量导出单元测试——Issue
func TestGiteeDealIssue(t *testing.T) {
	cfg := getNotUseProgressConfig(t)
	repoInfo := RepoInfo{
		RepoId:        7696525,
		RepoName:      "kernel",
		RepoGroupId:   5161228,
		RepoGroupName: "openEuler",
		//RepoName:         "aops-apollo",
		EnterpriseId:     5292411,
		CodeCloneHttpUrl: "https://gitee.com/openeuler/kernel.git",
		WikiEnable:       true,
	}

	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, GiteePlatformName)
	assert.Equal(t, err, nil)

	args := TaskArgs{
		TaskName:        TaskNameIssue,
		RepoInfo:        &repoInfo,
		ExportDataDir:   cfg.BaseConfig.ExportDataDir,
		Log:             log,
		Token:           cfg.BaseConfig.Token,
		EnterpriseToken: cfg.BaseConfig.EnterpriseToken,
		PageSize:        cfg.BaseConfig.PageSize,
		IncrementFlag:   false,
		TimeStamp:       cfg.BaseConfig.TimeStamp,
	}
	_ = args
	//err = giteeDealIssue(args)
	assert.Equal(t, err, nil)

	var issueList target.AtomGitIssueList
	dataArgs := DataArgs{
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		ExportPlatformName: GiteePlatformName,
		FileName:           BinFileNameIssues,
		//FilePathTmpl:       ExportDataFileTmpl,
		FilePathTmpl: "/Users/buyu/test/my_repo_data/issue.data",
		RepoInfo:     &repoInfo,
		Data:         &issueList,
	}

	_, err = ReadProtoData(dataArgs)
	assert.Equal(t, err, nil)

	fmt.Println(len(issueList.IssueList))
	fmt.Println(issueList.RepoId)
	fmt.Println(issueList.RepoName)
	fmt.Println(issueList.RepoGroupName)
	cnt := 0
	for _, val := range issueList.IssueList {
		//fmt.Println(val.Id, val.Title, time.Unix(val.UpdatedAt, 0), len(val.CommentList))
		////for _, cmt := range val.CommentList {
		////	fmt.Println(cmt.Id, cmt.Note, cmt.Author)
		////}
		//fmt.Println(val)
		//_ = val
		//fmt.Println("=================")
		check := true
		if len(val.CommentList) > 20 {
			for idx := range val.CommentList {
				if idx > 0 && val.CommentList[idx-1].CreatedAt < val.CommentList[idx].CreatedAt {
					check = false
				}
			}
			assert.Equal(t, check, true)
			cnt++
		}
		if cnt == 200 {
			break
		}

	}
}

// Gitee的Merge单元测试——User
func TestGiteeMergeAllUsers(t *testing.T) {
	cfg := getNotUseProgressConfig(t)

	err := giteeMergeAllUsers(cfg)
	assert.Equal(t, err, nil)

	var userList target.AtomGitUserList
	dataArgs := DataArgs{
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		ExportPlatformName: GiteePlatformName,
		FileName:           BinFileNameAllUsersProto,
		EnterpriseName:     cfg.BaseConfig.EnterpriseName,
		FilePathTmpl:       ExportEnterpriseFileTmpl,
		Data:               &userList,
	}

	_, err = ReadProtoData(dataArgs)
	assert.Equal(t, err, nil)

	fmt.Println(len(userList.UserList))
	for _, val := range userList.UserList {
		fmt.Println(val)
	}
}

// Gitee的Merge单元测试——单库合并Issues
func TestGiteeMergeOneRepoUsers(t *testing.T) {
	cfg := getNotUseProgressConfig(t)

	repoInfo := RepoInfo{
		RepoId:        7696525,
		RepoName:      "kernel",
		RepoGroupId:   5161228,
		RepoGroupName: "openEuler",
		//RepoName:         "aops-apollo",
		EnterpriseId:     5292411,
		CodeCloneHttpUrl: "https://gitee.com/openeuler/kernel.git",
		WikiEnable:       true,
	}

	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, GiteePlatformName)
	assert.Equal(t, err, nil)

	userMap := sync.Map{}

	args := MergeArgs{
		Repo:       &repoInfo,
		UserMap:    &userMap,
		BaseConfig: &cfg.BaseConfig,
		Log:        log,
	}

	err = giteeMergeUsersByRepo(args)
	assert.Equal(t, err, nil)

	userMap.Range(func(key, value any) bool {
		user := value.(*target.AtomGitUser)
		fmt.Println(user.String())
		return true
	})
}

// Gitee的Merge单元测试——MergeRequest
func TestGiteeMergeAllMergeRequests(t *testing.T) {
	cfg := getNotUseProgressConfig(t)

	err := giteeMergeAllMergeRequests(cfg)
	assert.Equal(t, err, nil)

	repoInfo := RepoInfo{
		RepoId:        7696525,
		RepoName:      "kernel",
		RepoGroupId:   5161228,
		RepoGroupName: "openEuler",
		//RepoName:         "aops-apollo",
		EnterpriseId:     5292411,
		CodeCloneHttpUrl: "https://gitee.com/openeuler/kernel.git",
		WikiEnable:       true,
	}

	var mrList target.AtomGitMergeRequestList
	dataArgs := DataArgs{
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		ExportPlatformName: GiteePlatformName,
		FileName:           BinFileNameMergeRequests,
		FilePathTmpl:       ExportDataFileTmpl,
		RepoInfo:           &repoInfo,
		Data:               &mrList,
	}

	_, err = ReadProtoData(dataArgs)
	assert.Equal(t, err, nil)

	fmt.Println(len(mrList.MergeRequestList))
	for _, val := range mrList.MergeRequestList {
		fmt.Println(val.Id, val.Title, time.Unix(val.UpdatedAt, 0), len(val.CommentList))
	}
}

// Gitee的Merge单元测试——单库合并MergeRequest
func TestGiteeMergeOneRepoMergeRequests(t *testing.T) {
	cfg := getNotUseProgressConfig(t)

	repoInfo := RepoInfo{
		RepoId:        7696525,
		RepoName:      "kernel",
		RepoGroupId:   5161228,
		RepoGroupName: "openEuler",
		//RepoName:         "aops-apollo",
		EnterpriseId:     5292411,
		CodeCloneHttpUrl: "https://gitee.com/openeuler/kernel.git",
		WikiEnable:       true,
	}

	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, GiteePlatformName)
	assert.Equal(t, err, nil)

	args := MergeArgs{
		Repo:       &repoInfo,
		UserMap:    nil,
		BaseConfig: &cfg.BaseConfig,
		Log:        log,
	}

	err = giteeMergeMergeRequestsByRepo(args)
	assert.Equal(t, err, nil)

	var mrList target.AtomGitMergeRequestList
	dataArgs := DataArgs{
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		ExportPlatformName: GiteePlatformName,
		FileName:           BinFileNameMergeRequestsMerge,
		FilePathTmpl:       ExportDataFileTmpl,
		RepoInfo:           &repoInfo,
		Data:               &mrList,
	}

	_, err = ReadProtoData(dataArgs)
	assert.Equal(t, err, nil)

	fmt.Println(len(mrList.MergeRequestList))
	//for _, val := range mrList.MergeRequestList {
	//	fmt.Println(val.Id, val.Title, time.Unix(val.UpdateAt, 0), len(val.CommentList))
	//}
}

// Gitee的Merge单元测试——Issue
func TestGiteeMergeAllIssues(t *testing.T) {
	cfg := getNotUseProgressConfig(t)

	err := giteeMergeAllIssues(cfg)
	assert.Equal(t, err, nil)

	repoInfo := RepoInfo{
		RepoId:        7696525,
		RepoName:      "kernel",
		RepoGroupId:   5161228,
		RepoGroupName: "openEuler",
		//RepoName:         "aops-apollo",
		EnterpriseId:     5292411,
		CodeCloneHttpUrl: "https://gitee.com/openeuler/kernel.git",
		WikiEnable:       true,
	}

	var issueList target.AtomGitIssueList
	dataArgs := DataArgs{
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		ExportPlatformName: GiteePlatformName,
		FileName:           BinFileNameIssues,
		FilePathTmpl:       ExportDataFileTmpl,
		RepoInfo:           &repoInfo,
		Data:               &issueList,
	}

	_, err = ReadProtoData(dataArgs)
	assert.Equal(t, err, nil)

	fmt.Println(len(issueList.IssueList))
	//for _, val := range issueList.IssueList {
	//	fmt.Println(val.Id, val.Title, time.Unix(val.UpdateAt, 0), len(val.CommentList))
	//}
}

// Gitee的Merge单元测试——单库合并Issues
func TestGiteeMergeOneRepoIssues(t *testing.T) {
	cfg := getNotUseProgressConfig(t)

	repoInfo := RepoInfo{
		RepoId:        7696525,
		RepoName:      "kernel",
		RepoGroupId:   5161228,
		RepoGroupName: "openEuler",
		//RepoName:         "aops-apollo",
		EnterpriseId:     5292411,
		CodeCloneHttpUrl: "https://gitee.com/openeuler/kernel.git",
		WikiEnable:       true,
	}

	log, err := makeAdapterLog(&cfg.BaseConfig, &repoInfo, GiteePlatformName)
	assert.Equal(t, err, nil)

	args := MergeArgs{
		Repo:       &repoInfo,
		UserMap:    nil,
		BaseConfig: &cfg.BaseConfig,
		Log:        log,
	}

	err = giteeMergeIssuesByRepo(args)
	assert.Equal(t, err, nil)

	var issueList target.AtomGitIssueList
	dataArgs := DataArgs{
		ExportDataDir:      cfg.BaseConfig.ExportDataDir,
		ExportPlatformName: GiteePlatformName,
		FileName:           BinFileNameIssuesMerge,
		FilePathTmpl:       ExportDataFileTmpl,
		RepoInfo:           &repoInfo,
		Data:               &issueList,
	}

	_, err = ReadProtoData(dataArgs)
	assert.Equal(t, err, nil)

	fmt.Println(len(issueList.IssueList))
	//for _, val := range issueList.IssueList {
	//	fmt.Println(val.Id, val.Title, time.Unix(val.UpdateAt, 0), len(val.CommentList))
	//}
}
